package com.xyc.redis.service;

import com.xyc.redis.cache.RedisHandler;
import com.xyc.redis.mapper.ProductMapper;
import com.xyc.redis.pojo.TbProduct;
import com.xyc.redis.service.inter.IRedisService;
import com.xyc.redis.vo.ProductVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RedisServiceImpl implements IRedisService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private RedisHandler redisHandler;
    @Override
    public List<ProductVO> selectProductList(Integer userId) {
        List<TbProduct> tbProducts = productMapper.selectProductList();//从数据库中获取数据

        Set<String> stringSet = redisHandler.queryKeysByExpression("pjname-zan-"+userId+"-*");

        ArrayList<ProductVO> voArrayList = new ArrayList<>();
        for (TbProduct product:tbProducts) {
            ProductVO productVO = new ProductVO(product);
            if (stringSet.contains("pjname-zan-"+userId+"-"+product.getProductId())){
                productVO.setZan(true);
            }
            voArrayList.add(productVO);
        }
        return voArrayList;
    }
}
